<?php
namespace app\store\controller\plugins;
use app\store\controller\Controller;
use app\store\model\Shop as ShopModel;
use app\store\model\WebSet;

/**
 * 门店控制器
 */
class Shop extends Controller
{
	/**
     * 门店地图
     */
    public function getpoint()
    {
		$values = WebSet::getItem('web');
		$this->view->engine->layout(false);
		$this->assign('wxmap_key', $values['wxmap']);
        return $this->fetch('getpoint');
    }
	
    /**
     * 列表
     */
    public function index()
    {
        $model = new ShopModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
		if($this->store['wxapp']['shop_mode']['value']==10){
			return $this->error('单门店版，无权新增门店');
		}
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        $model = new ShopModel;
		$model->add($this->postData('shop'));
		return $this->renderSuccess('添加成功', url('plugins.shop/index'));
        if ($model->add($this->postData('shop'))) {
            return $this->renderSuccess('添加成功', url('plugins.shop/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($shop_id)
    {
		return $this->renderError('暂不支持删除');
        $model = ShopModel::get($shop_id);
        if (!$model->remove()) {
            $error = $model->getError() ?: '添加失败';
			return $this->renderError($error);
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 编辑
     */
    public function edit($shop_id)
    {
        // 商品详情
        $model = ShopModel::detail($shop_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('shop'))) {
            return $this->renderSuccess('更新成功', url('plugins.shop/index'));
        }
        $error = $model->getError() ?: '更新失败';
        return $this->renderError($error);
    }
	
	/**
     * 状态编辑
     */
    public function status($shop_id)
    {
        $model = ShopModel::get($shop_id);
		$model->status['value'] ? $model->status = 0 : $model->status = 1;
		$model->save();
        return $this->renderSuccess('更新成功', url('plugins.shop/index'));
    }	
}
